package com.ruizhong.integratedmediawithoutc.controller;


import com.ruizhong.integratedmediawithoutc.service.TPlanService;
import com.ruizhong.integratedmediawithoutc.util.CMD;
import com.ruizhong.integratedmediawithoutc.util.Result;
import com.ruizhong.integratedmediawithoutc.util.ResultCode;
import com.ruizhong.integratedmediawithoutc.util.TCPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/plan/api")
@CrossOrigin
public class PlanController {

    @Autowired
    private TPlanService tPlanService;

    private static String[] weekdays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    @PostMapping("/addPlan/{userName}")
    public Result addPlan(@RequestBody Map<String, Object> dataMap, @PathVariable(value = "userName") String userName) {
        tPlanService.addPlan(dataMap);
        return new Result(ResultCode.success, null, null);
    }

    @PostMapping("/getPlanList")
    public Result getPlanList(@RequestBody Map<String, Object> conditionMap) {
        Map<String, Object> queryPlanListMap = TCPUtil.send(conditionMap);
        conditionMap.put("command", CMD.QueryPL.ordinal());
        Map<String, Object> queryPLMap = TCPUtil.send(conditionMap);
        ArrayList<Map<String, Object>> data = (ArrayList<Map<String, Object>>) queryPLMap.get("data");
        Map<String, Map<String, Object>> idMapName = data.stream().collect(Collectors.toMap(t -> t.get("id").toString(), t -> t));
        ArrayList<Map<String, Object>> planList = (ArrayList<Map<String, Object>>) queryPlanListMap.get("data");
        for (Map<String, Object> plan : planList) {
            Map<String, Object> program = idMapName.get(plan.get("pl_id").toString());
            plan.put("programType", "计划");
            plan.put("programs", Collections.emptyList());

            if (program == null) {
                continue;
            }
            ArrayList<Map<String, Object>> programItems = (ArrayList<Map<String, Object>>) program.get("programs");
            Integer totalMS = 0;
            for (Map<String, Object> programItem : programItems) {
                totalMS += (Integer) programItem.get("duration");
            }
            int freq = (int) plan.get("freq");
            Integer minute = totalMS / (1000 * 60);
            Integer mSecond = totalMS % (1000 * 60);
            plan.put("pgDuration", minute + "分" + (mSecond / 1000) + "秒");
            plan.put("program_name", program.get("program_name"));
            plan.put("programs", program.get("programs"));
            plan.put("showSonPrograms", false);

            String days = plan.get("days").toString();
            switch (freq) {
                case 0:
                    StringBuilder sb = new StringBuilder(days);
                    sb.insert(4, "年");
                    sb.insert(7, "月");
                    sb.insert(10, "日");
                    plan.put("days", sb.toString());
                    break;
                case 1:
                    plan.put("days", "\\");
                    break;
                case 2:
                    plan.put("days", weekdays[Integer.parseInt(days)]);
                    break;
            }
            int timeInt = (int) plan.get("time");
            LocalTime time = LocalTime.ofSecondOfDay(timeInt);
            plan.put("time", String.format("%02d:%02d", time.getHour(), time.getMinute()));
        }

        return new Result(ResultCode.success, null, queryPlanListMap);
    }

    @PostMapping("/deletePlan")
    public Result deletePlan(@RequestBody Map<String, Object> conditionMap) {
        Map<String, Object> resMap = TCPUtil.send(conditionMap);

        return new Result(ResultCode.success, null, resMap);
    }

}
